package com.ava.avamedia.lottery.mienbac;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.ava.avamedia.R;
import com.ava.avamedia.model.MBResult;
import com.ava.avamedia.util.Constants;
import com.congnt.library.utility.DialogUtils;
import com.congnt.library.utility.HTTPClientUtil;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class MBResultsActivity extends Activity {
	private TextView dayTV;
	private TextView specialTV;
	private TextView firstTV;
	private TextView secondTV;
	private TextView thirdTV;
	private TextView fourTV;
	private TextView fifthTV;
	private TextView sixTV;
	private TextView sevenTV;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_result);
		if (HTTPClientUtil.isInternetConection()) {
			new AsyncTaskDownloadResult().execute();
		}
		dayTV = (TextView) findViewById(R.id.activity_result_day);
		specialTV = (TextView) findViewById(R.id.activity_result_db);
		firstTV = (TextView) findViewById(R.id.activity_result_first);
		secondTV = (TextView) findViewById(R.id.activity_result_second);
		thirdTV = (TextView) findViewById(R.id.activity_result_third);
		fourTV = (TextView) findViewById(R.id.activity_result_four);
		fifthTV = (TextView) findViewById(R.id.activity_result_five);
		sixTV = (TextView) findViewById(R.id.activity_result_six);
		sevenTV = (TextView) findViewById(R.id.activity_result_seven);
	}
	public void initResult(MBResult mbResult){
		dayTV.setText(mbResult.day);
		specialTV.setText(mbResult.special);
		firstTV.setText(mbResult.first);
		secondTV.setText(mbResult.second);
		thirdTV.setText(mbResult.third);
		fourTV.setText(mbResult.fourth);
		fifthTV.setText(mbResult.fifth);
		sixTV.setText(mbResult.sixth);
		sevenTV.setText(mbResult.seventh);
	}
	private static final String TAG = MBResultsActivity.class.getSimpleName();

	private ProgressDialog dialog;
	class AsyncTaskDownloadResult extends AsyncTask<Void, Void, MBResult> {

		@Override
		protected MBResult doInBackground(Void... params) {
			try {
				Document doc = Jsoup.connect(Constants.KQ_MB_URL)
						.userAgent(Constants.USER_AGENT).get();
				Elements elementDay = doc
						.body()
						.select("form#aspnetForm > div.bg_body > div.conten > div.kqxs > div.boder > *");
				Element elementResult = elementDay.get(3);
				Log.i(TAG, "----------> " + elementResult.getElementsByClass("main_table").select("span#msgXS > span#Ldate").text());
				return getMBResultByDivTag(elementResult);
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			dialog = DialogUtils.showProgress(MBResultsActivity.this, "Đang tải kết quả từ trang ketquaday.vn.......");
		}
		@Override
		protected void onPostExecute(MBResult result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			dialog.dismiss();
			initResult(result);
		}
	}

	public String getResultByElement(Element result) {
		return result.child(1).text();
	}
	public MBResult getMBResultByDivTag(Element elementDiv){
		MBResult mbResult = new MBResult();
		Elements elementTableResult = elementDiv.child(1)
				.select("table > tbody > tr ").get(0)
				.select("table > tbody > *");

		Element specialResult = elementTableResult.get(0);
		Element firstResult = elementTableResult.get(1);
		Element secondResult = elementTableResult.get(2);
		Element thirdResult = elementTableResult.get(3);
		Element fourthResult = elementTableResult.get(4);
		Element fifthResult = elementTableResult.get(5);
		Element sixthResult = elementTableResult.get(6);
		Element seventhResult = elementTableResult.get(7);
		mbResult.day = elementDiv.getElementsByClass("main_table").select("span#msgXS > span#Ldate").text();
		mbResult.special = getResultByElement(specialResult);
		mbResult.first = getResultByElement(firstResult);
		mbResult.second = getResultByElement(secondResult);
		mbResult.third = getResultByElement(thirdResult);
		mbResult.fourth = getResultByElement(fourthResult);
		mbResult.fifth = getResultByElement(fifthResult);
		mbResult.sixth= getResultByElement(sixthResult);
		mbResult.seventh= getResultByElement(seventhResult);
		return mbResult;
	}
}
